package com.etc.ssm.service.impl;


import com.etc.ssm.dao.GoodsMapper;
import com.etc.ssm.entity.Goods;
import com.etc.ssm.entity.GoodsExample;
import com.etc.ssm.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 
 * <p>
 * Title: GoodsServiceImpl
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * @author knowno
 * 
 * @date 2021年3月9日
 * 
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	GoodsMapper goodsMapper;

	/**
	 * 分页查询全部商品
	 */
	@Override
	//@Cacheable(cacheNames = "com.etc.ssm.service.impl.showGoods")
	public PageInfo<Goods> showGoods(int page, int pageSize, String keywords) {
		if (page < 1) {
			page = 1;
		}
		// 创建example对象
		GoodsExample example = new GoodsExample();
		example.createCriteria().andGoodsnameLike("%" + keywords + "%");

		// 分页查询
		PageHelper.startPage(page, pageSize);
		List<Goods> list = goodsMapper.selectByExample(example);

		// 返回一个pageInfo对象包含了分页有关的各种属性，相当于我们以前的pageData
		PageInfo<Goods> pd = new PageInfo<Goods>(list);
		return pd;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * <p>
	 * Title: showGoodsDetail
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param page
	 * @param pageSize
	 * @param keywords
	 * @return
	 * 
	 * @see
	 * 
	 */
	@Override
	public PageInfo<Goods> showGoodsDetail(int page, int pageSize, String keywords) {
		// TODO Auto-generated method stub
		if (page < 1) {
			page = 1;
		}
		// 创建example对象
		GoodsExample example = new GoodsExample();
		example.createCriteria().andGoodsnameLike("%" + keywords + "%");

		// 分页查询
		PageHelper.startPage(page, pageSize);
		List<Goods> list = goodsMapper.selectByLike(example);

		// 返回一个pageInfo对象包含了分页有关的各种属性，相当于我们以前的pageData
		PageInfo<Goods> pd = new PageInfo<Goods>(list);
		return pd;
	}
	
	
	/** (non-Javadoc)
	
	* <p>Title: getGoodsById</p>
	
	* <p>Description: </p>
	
	* @param goodsid
	* @return
	
	* @see
	
	*/
	@Override
	public Goods getGoodsById(int goodsid) {
		// TODO Auto-generated method stub
		return goodsMapper.selectByPrimaryKey(goodsid);
	}


	@Override
	public int updateGoods(Goods goods) {
		return goodsMapper.updateByPrimaryKey(goods);
	}
}
